
class ReadFile:

	def __init__(self, nameSeries, sizeTestSeries):
		self.nameSeries = nameSeries
		self.sizeTestSeries = sizeTestSeries

	def readTypeOne(self):
	
		for i in range(1, 21):
			try:
				FileInput = open("../readTypeOne_%s_fitness_%d.txt" %(self.nameSeries, i), "w")
			except:
				print "Arquivo nao nao pode ser criado!"
				continue
			generalList = []
			for j in range(1, 31):
				try:
					FileOutput = open("melhor_ind_simulacao_%s_%d_fitness_%d.txt" %(self.nameSeries, j, i), "r")
				except:
					print "Arquivo nao encontrado ou corrompido!"
					continue
				readingList = []
				readingList = FileOutput.readlines()

				generalList.append( readingList[36].replace("\n", "").rsplit(" ")[2] + " " + readingList[44].replace("\n", "").replace("| ", "") + " " + \
					readingList[37].replace("\n", "").rsplit(" ")[4] + " " + readingList[47].replace("\n", "").replace("| ", "") + " " + \
					readingList[38].replace("\n", "").rsplit(" ")[4] + " " + readingList[50].replace("\n", "").replace("| ", "") + " " + "\n")

				FileOutput.close()

			FileInput.writelines(generalList)
			FileInput.close()

	def readTypeTwo(self):

		for i in range(1, 21):
			try:
				FileInput = open("../readTypeTwo_%s_fitness_%d.txt" %(self.nameSeries, i), "w")
			except:
				print "Arquivo nao nao pode ser criado!"
				continue
			generalList = [""]*self.sizeTestSeries
			for j in range(1, 31):
				try:
					FileOutput = open("melhor_ind_simulacao_%s_%d_fitness_%d.txt" %(self.nameSeries, j, i), "r")
				except:
					print "Arquivo nao encontrado ou corrompido!"
					continue
				readingList = []
				readingList = FileOutput.readlines()

				if j == 1 and i == 1:
					position = 0
					ver = 0
					for x in readingList:
						if x == "Series Previstas\n": ver = 1
						if x == "Teste\n" and ver == 1: ver = 2
					
						if ver != 2: position += 1
					position += 2
		
				for x in range(self.sizeTestSeries):
					generalList[x] += readingList[x + position].replace(";\n", "") + " "

				FileOutput.close()

			for j in range(self.sizeTestSeries):
				generalList[j] += "\n"

			FileInput.writelines(generalList)

			FileInput.close()
